home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / NumberFormatDemo.java < prev    next >
Text File  |  1998-09-15  |  29KB  |  983 lines

  1. /*
  2.  * @(#)NumberFormatDemo.java     1.1 98/06/16
  3.  *
  4.  * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  5.  * (C) Copyright IBM Corp. 1996 - All Rights Reserved
  6.  *
  7.  * Portions copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  8.  *
  9.  *   The original version of this source code and documentation is copyrighted
  10.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  11.  * materials are provided under terms of a License Agreement between Taligent
  12.  * and Sun. This technology is protected by multiple US and International
  13.  * patents. This notice and attribution to Taligent may not be removed.
  14.  *   Taligent is a registered trademark of Taligent, Inc.
  15.  *
  16.  * Permission to use, copy, modify, and distribute this software
  17.  * and its documentation for NON-COMMERCIAL purposes and without
  18.  * fee is hereby granted provided that this copyright notice
  19.  * appears in all copies. Please refer to the file "copyright.html"
  20.  * for further important copyright and licensing information.
  21.  *
  22.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  23.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  24.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  25.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  26.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  27.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  28.  *
  29.  */
  30.  
  31. import java.applet.Applet;
  32. import java.awt.event.*;
  33. import java.awt.*;
  34. import java.util.*;
  35. import java.lang.*;
  36. import java.text.*;
  37.  
  38. /**
  39.  * Concrete class for demonstrating how to convert binary numbers into text
  40.  * strings for meaningful display.
  41.  * The following is the instruction on how to run the number format demo.
  42.  * <p>
  43.  * ===================
  44.  * <P>Number formatters convert binary numbers (integer or float data types)
  45.  * into text strings for meaningful display. The number formatters:
  46.  * <UL>
  47.  * <LI>Provide control over every aspect of the display, including separator
  48.  * characters, optional digits, maximum and minimum decimal and integer places,
  49.  * and optional prefixes and suffixes </LI>
  50.  * <LI>Can produce currency and percentage formats and can be extended to
  51.  * produce other formats </LI>
  52.  * <LI>Allow you to specify new formats with string patterns and can retrieve
  53.  * the normalized pattern </LI>
  54.  * <LI>Can parse anything they can format, for use when converting text input
  55.  * into binary data </LI>
  56.  * </UL>
  57.  * <P><CENTER>
  58.  * <A HREF="#localized">Localized Formats</A>  
  59.  * <A HREF="#patterns">Creating Formats with Patterns </A>  
  60.  * <A HREF="#parsing">Parsing Text </A>
  61.  * </CENTER></P>
  62.  * <HR>
  63.  * <H3><A NAME="localized">Localized Formats </A></H3>
  64.  * <P>The number formatters let you format numbers in the ways that are
  65.  * appropriate for any user's conventions, including country-specific formats
  66.  * such as currencies.  You can also specify other formatting options such as
  67.  * minimum and maximum number of digits displayed.
  68.  * </P>
  69.  * <P> The applet displays the number in two formats—the Java 1.0
  70.  * representation ("Java 1.0 Number"), and the localized format
  71.  * generated by the number formatters ("New Number").
  72.  * </P>
  73.  * <TABLE WIDTH=100% CELLPADDING=5>
  74.  * <TR>
  75.  * <TH ALIGN=left VALIGN=bottom>To See This...</TH>
  76.  * <TH ALIGN=left VALIGN=bottom>Do This...</TH>
  77.  * </TR>
  78.  * <TR>
  79.  * <TD WIDTH=50% VALIGN=top> Localized number formats.  In the international
  80.  * string, the separator characters change to reflect the conventions used by
  81.  * different countries.
  82.  * </TD>
  83.  * <TD VALIGN=top>
  84.  * <TABLE CELLSPACING=0 CELLPADDING=0>
  85.  * <TR>
  86.  * <TD WIDTH=10 VALIGN=top> 1.
  87.  * </TD>
  88.  * <TD> Pull down the Locale menu
  89.  * </TD>
  90.  * </TR>
  91.  * <TR>
  92.  * <TD WIDTH=10 VALIGN=top> 2.
  93.  * </TD>
  94.  * <TD> Try several different locales with the up and down arrow keys
  95.  * (on Windows) or the mouse button (on Macintosh)
  96.  * </TD>
  97.  * </TR>
  98.  * </TABLE>
  99.  * </TD>
  100.  * </TR>
  101.  * <TR>
  102.  * <TD WIDTH=50% VALIGN=top> Localized currency formats.  The formatter adds
  103.  * a prefix or suffix to indicate the currency unit.  It also uses a minus sign
  104.  * or parentheses to indicate negative numbers, as appropriate.
  105.  * </TD>
  106.  * <TD VALIGN=top>
  107.  * <TABLE CELLSPACING=0 CELLPADDING=0>
  108.  * <TR>
  109.  * <TD WIDTH=10 VALIGN=top> 1.
  110.  * </TD>
  111.  * <TD> Click on the <I> Currency </I> button
  112.  * </TD>
  113.  * </TR>
  114.  * <TR>
  115.  * <TD WIDTH=10 VALIGN=top> 2.
  116.  * </TD>
  117.  * <TD> Pull down the Locale menu and try several different locales
  118.  * </TD>
  119.  * </TR>
  120.  * </TABLE>
  121.  * </TD>
  122.  * </TR>
  123.  * <TR>
  124.  * <TD WIDTH=50% VALIGN=top> Setting formatting options, for example,
  125.  * specifying a precise  number of decimal places.
  126.  * </TD>
  127.  * <TD VALIGN=top>
  128.  * <TABLE CELLSPACING=0 CELLPADDING=0>
  129.  * <TR>
  130.  * <TD WIDTH=10 VALIGN=top> 1.
  131.  * </TD>
  132.  * <TD> Type "3" in the <I>Min </I> field for <I> Decimal Count </I>
  133.  * —the formatter appends zeroes to display the specified number of
  134.  * decimal places
  135.  * </TD>
  136.  * </TR>
  137.  * </TABLE>
  138.  * </TD>
  139.  * </TR>
  140.  * </TABLE>
  141.  * <HR WIDTH=50% ALIGN=left>
  142.  * <H3><A NAME="patterns">Creating Formats with Patterns </A></H3>
  143.  * <P> A string pattern can control the output format for a number formatter,
  144.  * making it easy for you to edit a format or provide an editing mechanism
  145.  * for end users.  The pattern also changes to reflect any changes you make
  146.  * programmatically.
  147.  * </P>
  148.  * <P> Patterns can either be invariant (for use only by programmers) or
  149.  * localized (for use by end users).  Click on the <I> Localized Pattern </I>
  150.  * box to change the state.
  151.  * <TABLE WIDTH=100% CELLPADDING=5>
  152.  * <TR>
  153.  * <TH ALIGN=left VALIGN=bottom>To See This...</TH>
  154.  * <TH ALIGN=left VALIGN=bottom>Do This...</TH>
  155.  * </TR>
  156.  * <TR>
  157.  * <TD WIDTH=50% VALIGN=top> You can edit the pattern to create a new format.
  158.  * </TD>
  159.  * <TD VALIGN=top>
  160.  * <TABLE CELLSPACING=0 CELLPADDING=0>
  161.  * <TR>
  162.  * <TD WIDTH=10 VALIGN=top> 1.
  163.  * </TD>
  164.  * <TD> Select the <I> Default Format </I> button and the <I>English
  165.  * (United States) </I> locale
  166.  * </TD>
  167.  * </TR>
  168.  * <TR>
  169.  * <TD WIDTH=10 VALIGN=top> 2.
  170.  * </TD>
  171.  * <TD> Increase the maximum number for <I>Decimal Count</I>
  172.  * </TD>
  173.  * </TR>
  174.  * <TR>
  175.  * <TD WIDTH=10 VALIGN=top> 3.
  176.  * </TD>
  177.  * <TD> Delete the "#" characters at the end of the <I>Pattern
  178.  * </I> field and type in zeroes—the decimal places in the <I>New Number
  179.  * </I> field change to zeroes
  180.  * </TD>
  181.  * </TR>
  182.  * </TABLE>
  183.  * </TD>
  184.  * </TR>
  185.  * <TR>
  186.  * <TD WIDTH=50% VALIGN=top> You can edit the prefix and suffix strings, and
  187.  * create different patterns for positive and negative numbers.
  188.  * </TD>
  189.  * <TD VALIGN=top>
  190.  * <TABLE CELLSPACING=0 CELLPADDING=0>
  191.  * <TR>
  192.  * <TD WIDTH=10 VALIGN=top> 1.
  193.  * </TD>
  194.  * <TD> Type a plus sign ("+") in the <I> Prefix </I> field for
  195.  * positive numbers to modify the positive number pattern
  196.  * </TD>
  197.  * </TR>
  198.  * <TR>
  199.  * <TD WIDTH=10 VALIGN=top> 2.
  200.  * </TD>
  201.  * <TD> Delete the minus sign from the Java 1.0 number
  202.  * </TD>
  203.  * </TR>
  204.  * </TABLE>
  205.  * </TD>
  206.  * </TR>
  207.  * </TABLE>
  208.  * <HR WIDTH=50% ALIGN=left>
  209.  * <H3><A NAME="parsing">Parsing Text </A></H3>
  210.  * <P>The number formatters not only format for any language, but can parse
  211.  * numbers displayed in any language back into binary format.
  212.  * </P>
  213.  * <TABLE WIDTH=100% CELLPADDING=5>
  214.  * <TR>
  215.  * <TH ALIGN=left VALIGN=bottom>To See This...</TH>
  216.  * <TH ALIGN=left VALIGN=bottom>Do This...</TH>
  217.  * </TR>
  218.  * <TR>
  219.  * <TD WIDTH=50% VALIGN=top> Changes to the international number are parsed
  220.  * and reflected in the <I> Java 1.0 Number </I> field.
  221.  * </TD>
  222.  * <TD VALIGN=top>
  223.  * <TABLE CELLSPACING=0 CELLPADDING=0>
  224.  * <TR>
  225.  * <TD WIDTH=10 VALIGN=top> 1.
  226.  * </TD>
  227.  * <TD> Select the thousands separator character in the <I>New Number </I>
  228.  * field (the comma if you are still in the U.S. locale) and replace it with
  229.  * a number
  230.  * </TD>
  231.  * </TR>
  232.  * </TABLE>
  233.  * </TD>
  234.  * </TR>
  235.  * </TABLE>
  236.  * <P>The parsing mechanism is somewhat lenient. For example, a thousands
  237.  * separator character can be misplaced and the parser still returns the
  238.  * correct value. </P>
  239.  * <P>Though not yet demonstrated here, the number formatters also handle
  240.  * IEEE number symbols such as the infinity sign and NaN (for example, 0/0),
  241.  * creating the correct results for both formatting and parsing. The formatters
  242.  * also return the alignment point so you can line up the end of integer
  243.  * portions correctly even without monospace fonts or with strange number
  244.  * conventions. </P>
  245.  * <P>You can type in other numbers in either number field or enter a
  246.  * completely new pattern to  see different formatting behaviors.  Try it out!
  247.  * </P>
  248.  * @see        java.util.Format
  249.  * @see        java.util.NumberFormat
  250.  * @version    1.1 11/23/96
  251.  * @author     Laura Werner, Mark Davis
  252. */
  253. // Change history:  Helena Shih 091996: Added comments prior spec review.
  254. public class NumberFormatDemo extends DemoApplet
  255. {
  256.     /**
  257.      * The main function which defines the behavior of the NumberFormatDemo
  258.      * applet when an applet is started.
  259.      */
  260.     public static void main(String argv[]) {
  261.         DemoApplet.showDemo(new NumberFormatFrame(null));
  262.     }
  263.  
  264.     /**
  265.      * This creates a NumberFormatFrame for the demo applet.
  266.      */
  267.     public Frame createDemoFrame(DemoApplet applet) {
  268.         return new NumberFormatFrame(applet);
  269.     }
  270. }
  271.  
  272. /**
  273.  * A Frame is a top-level window with a title. The default layout for a frame
  274.  * is BorderLayout.  The NumberFormatFrame class defines the window layout of
  275.  * NumberFormatDemo.
  276.  */
  277. class NumberFormatFrame extends Frame implements KeyListener, ItemListener, WindowListener 
  278. {
  279.     /**
  280.      * Constructs a new NumberFormatFrame that is initially invisible.
  281.      */
  282.     public NumberFormatFrame(DemoApplet applet)
  283.     {
  284.         super("Number Formatting Demo");
  285.         this.applet = applet;
  286.     addWindowListener(this);
  287.         init();
  288.         start();
  289.     }
  290.  
  291.     /**
  292.      * Initializes the applet. You never need to call this directly, it
  293.      * is called automatically by the system once the applet is created.
  294.      */
  295.     public void init()
  296.     {
  297.         //Get all locales for debugging, but only get G7 locales for demos.
  298.         if (DEBUG == true)
  299.              locales = NumberFormat.getAvailableLocales();
  300.         else locales = Utility.getG7Locales();
  301.  
  302.         buildGUI();
  303.  
  304.         // Stick the names of the locales into the locale popup menu
  305.         Locale displayLocale = Locale.getDefault();
  306.         for (int i = 0; i < locales.length; i++) {
  307.             if (locales[i].getCountry().length() > 0) {
  308.                 localeMenu.addItem( locales[i].getDisplayName() );
  309.                 if (locales[i].equals(Locale.getDefault())) {
  310.                     localeMenu.select(i);
  311.                 }
  312.             }
  313.         }
  314.  
  315.         // For starters, use the default format for the selected locale in the
  316.         // menu
  317.         setFormatFromLocale();
  318.     }
  319.  
  320.     /**
  321.      * Called to start the applet. You never need to call this method
  322.      * directly, it is called when the applet's document is visited.
  323.      */
  324.     public void start()
  325.     {
  326.         inputText.setText("-1234.56");
  327.         clickedRightButton();
  328.     }
  329.  
  330.  
  331.   /* ItemListener method */
  332.   public void itemStateChanged(ItemEvent e) {
  333.  
  334.     if (e.getSource() == getPercent) {
  335.       clickedGetPercent();
  336.     }
  337.     else
  338.     if (e.getSource() == getCurrency) {
  339.       clickedGetCurrency();
  340.     }
  341.     else
  342.     if (e.getSource() == getInstance) {
  343.       clickedGetDefault();
  344.     }
  345.     else
  346.     if (e.getSource() == getLocalized) {
  347.       isLocalized = getLocalized.getState();
  348.       handleNewFormat();
  349.     }
  350.     else
  351.     if (e.getSource() == localeMenu) {
  352.       selectedLocaleMenu();
  353.     }
  354.   }
  355.  
  356.   /* Key Listener methods */
  357.   public void keyPressed(KeyEvent e) {
  358.   }
  359.  
  360.   public void keyReleased(KeyEvent e) {
  361.     if (e.getSource() == inputText) {
  362.       inputChanged();
  363.     }
  364.     else
  365.     if (e.getSource() == patternText) {
  366.       patternChanged();
  367.     }
  368.     else
  369.     if (e.getSource() == outputText) {
  370.       outputChanged();
  371.     }
  372.     else
  373.     if (e.getSource() == posSuffix) {
  374.       posSuffixChanged();
  375.     }
  376.     else
  377.     if (e.getSource() == posPrefix) {
  378.       posPrefixChanged();
  379.     }
  380.     else
  381.     if (e.getSource() == negSuffix) {
  382.       negSuffixChanged();
  383.     }
  384.     else
  385.     if (e.getSource() == negPrefix) {
  386.       negPrefixChanged();
  387.     }
  388.     else
  389.     if (e.getSource() == decMax) {
  390.       decMaxChanged();
  391.     }
  392.     else
  393.     if (e.getSource() == decMin) {
  394.       decMinChanged();
  395.     }
  396.     else
  397.     if (e.getSource() == intMax) {
  398.       intMaxChanged();
  399.     }
  400.     else
  401.     if (e.getSource() == intMin) {
  402.       intMinChanged();
  403.     }
  404.   }
  405.  
  406.   public void keyTyped(KeyEvent e) {
  407.   }
  408.  
  409.   /* Window Listener methods */
  410.   public void windowClosed(WindowEvent e) {
  411.   }
  412.  
  413.   public void windowDeiconified(WindowEvent e) {
  414.   }
  415.  
  416.   public void windowIconified(WindowEvent e) {
  417.   }
  418.  
  419.   public void windowActivated(WindowEvent e) {
  420.   }
  421.  
  422.   public void windowDeactivated(WindowEvent e) {
  423.   }
  424.  
  425.   public void windowOpened(WindowEvent e) {
  426.   }
  427.  
  428.   public void windowClosing(WindowEvent e) {
  429.     setVisible(false);
  430.     dispose();
  431.  
  432.     if (applet != null) {
  433.       applet.demoClosed();
  434.     } else System.exit(0);
  435.  
  436.   }
  437.  
  438.     /**
  439.      * This function is called when it is necessary to create a new number
  440.      * format.  For example, when a new locale is chosen, a new number
  441.      * format for that locale needs to be created.  Or, when a number
  442.      * format attributes have been changed.
  443.      */
  444.     public void handleNewFormat()
  445.     {
  446.         if (isLocalized) {
  447.                workaroundSetText(patternText,format.toLocalizedPattern() );
  448.         } else workaroundSetText(patternText,format.toPattern() );
  449.  
  450.         // force the text in the output area to use the new format
  451.         clickedRightButton();
  452.     }
  453.  
  454.     /**
  455.      * A new number format needs to be created if the attributes are changed.
  456.      */
  457.     public void handleAttributeChange()
  458.     {
  459.         handleNewFormat();
  460.     }
  461.     /**
  462.      * Called when a formatting event has occured.
  463.      */
  464.     public boolean handleFormatting(Event evt, Object obj)
  465.     {
  466.         return true;
  467.     }
  468.  
  469.  
  470.     /**
  471.      * Create a new number format if a new locale has been selected.
  472.      */
  473.     public void setFormatFromLocale() {
  474.         // Find the locale corresponding to the currently selected menu item
  475.         int index = localeMenu.getSelectedIndex();
  476.         Locale aLocale = locales[index];
  477.  
  478.         if (getPercent.getState()) {
  479.             format = (DecimalFormat) NumberFormat.getPercentInstance(aLocale);
  480.         } else if (getCurrency.getState()) {
  481.             format = (DecimalFormat) NumberFormat.getCurrencyInstance(aLocale);
  482.         } else {
  483.             format = (DecimalFormat) NumberFormat.getInstance(aLocale);
  484.         }
  485.  
  486.         handleNewFormat();
  487.  
  488.         updateAttributes();
  489.     }
  490.  
  491.     /**
  492.      * This is used to avoid goofy updates and misplaced cursors
  493.      */
  494.     public void workaroundSetText(TextComponent area, String newText) {
  495.         String foo = workaround(area.getText());
  496.         if (foo.equals(newText)) return;
  497.         area.setText(newText);
  498.         //if (area.gotFocus())
  499.         //    area.select(newText.length(),newText.length());
  500.     }
  501.  
  502.     /**
  503.      * Update the attributes.
  504.      */
  505.     public void updateAttributes() {
  506.         workaroundSetText(intMin,toString(format.getMinimumIntegerDigits()) );
  507.         workaroundSetText(intMax,toString(format.getMaximumIntegerDigits()) );
  508.         workaroundSetText(decMin,toString(format.getMinimumFractionDigits()) );
  509.         workaroundSetText(decMax,toString(format.getMaximumFractionDigits()) );
  510.  
  511.         workaroundSetText(negPrefix,format.getNegativePrefix() );
  512.         workaroundSetText(negSuffix,format.getNegativeSuffix() );
  513.         workaroundSetText(posPrefix,format.getPositivePrefix() );
  514.         workaroundSetText(posSuffix,format.getPositiveSuffix() );
  515.     }
  516.  
  517.     /**
  518.      * Get the default number formatter.
  519.      */
  520.     public void clickedGetDefault() {
  521.         setFormatFromLocale();
  522.     }
  523.     /**
  524.      * Update the number formatter with currency attribute.
  525.      */
  526.     public void clickedGetCurrency() {
  527.         setFormatFromLocale();
  528.     }
  529.     /**
  530.      * Update the number formatter with percentage attribute.
  531.      */
  532.     public void clickedGetPercent() {
  533.         setFormatFromLocale();
  534.     }
  535.     /**
  536.      * Update the number formatter when a new locale is selected.
  537.      */
  538.     public void selectedLocaleMenu() {
  539.         setFormatFromLocale();
  540.     }
  541.     /**
  542.      * If the user changes the input number, format it immediately
  543.      */
  544.     public void inputChanged() {
  545.         clickedRightButton();
  546.     }
  547.     /**
  548.      * If the user changes the output number, format it immediately
  549.      */
  550.     public void outputChanged() {
  551.         clickedLeftButton();
  552.     }
  553.  
  554.     /**
  555.      * Update the text in the input area to use the new number format.
  556.      */
  557.     public void clickedRightButton() {
  558.         String leftString = workaround(inputText.getText());
  559.         Number num;
  560.  
  561.         try {
  562.             num = new Double(leftString);
  563.         }
  564.         catch (IllegalArgumentException e) {
  565.             outputText.setText("");
  566.             inputText.selectAll();
  567.             return;
  568.         }
  569.         workaroundSetText(outputText,format.format(num));
  570.     }
  571.  
  572.     /**
  573.      * Update the text in the output area to use the new number format.
  574.      */
  575.     public void clickedLeftButton() {
  576.         String rightString = workaround(outputText.getText());
  577.         Number num;
  578.         ParsePosition status = new ParsePosition(1);
  579.  
  580.         try {
  581.             num = format.parse(rightString, status);
  582.         }
  583.         catch (IllegalArgumentException e) {
  584.             errorText("Exception: " + e.getClass() + e.getMessage());
  585.             inputText.setText("");
  586.             outputText.selectAll();
  587.             return;
  588.         }
  589.         if (status.getIndex() != rightString.length()) {
  590.             errorText("excess text: " +
  591.                       rightString.substring(status.getIndex(),
  592.                                             rightString.length()));
  593.         }
  594.         workaroundSetText(inputText, num.toString() );
  595.     }
  596.  
  597.     /**
  598.      * When the user changes the field containing the pattern, a new format
  599.      * needs to be constructed from the pattern, then redisplay everything.
  600.      */
  601.     public void patternChanged() {
  602.         String foo = workaround(patternText.getText());
  603.         try {
  604.             if (isLocalized) {
  605.                    format.applyLocalizedPattern(foo);
  606.                    errorText("Pattern: " + foo + " -> " +
  607.                          format.toLocalizedPattern());
  608.             } else {
  609.                format.applyPattern(foo);
  610.                errorText("Pattern: " + foo + " -> " +
  611.                            format.toPattern());
  612.             }
  613.         } catch (Exception bar) {
  614.             errorText("Exception: " + bar.getClass() + bar.getMessage());
  615.         }
  616.         updateAttributes();
  617.         clickedRightButton();
  618.  
  619.     }
  620.     //------------------------------------------------------------
  621.     // package private
  622.     //------------------------------------------------------------
  623.     void addWithFont(Container container, Component foo, Font font) {
  624.         if (font != null)
  625.             foo.setFont(font);
  626.         container.add(foo);
  627.     }
  628.  
  629.     //{{DECLARE_CONTROLS
  630.     Panel localePanel;
  631.     CheckboxGroup group1;
  632.     Label label1;
  633.     Label label2;
  634.     Label label3;
  635.     Choice localeMenu;
  636.     Label localeLabel;
  637.     TextField inputText;
  638.     TextField outputText;
  639.     //Button rightButton;
  640.     //Button leftButton;
  641.     TextField patternText;
  642.     Label label4;
  643.     TextField negPrefix;
  644.     TextField negSuffix;
  645.     TextField posPrefix;
  646.     TextField posSuffix;
  647.     Label label5;
  648.     Label label6;
  649.     Label label7;
  650.     Label label8;
  651.     TextField intMin;
  652.     TextField intMax;
  653.     TextField decMin;
  654.     TextField decMax;
  655.     Label label9;
  656.     Label label10;
  657.     Label label11;
  658.     Label label12;
  659.     Checkbox getInstance;
  660.     Checkbox getCurrency;
  661.     Checkbox getPercent;
  662.     Checkbox getLocalized;
  663.     Label label13;
  664.     Label label14;
  665.     //}}
  666.     //------------------------------------------------------------
  667.     // private
  668.     //------------------------------------------------------------
  669.     private void buildGUI()
  670.     {
  671.         //{{INIT_CONTROLS
  672.         setBackground(Color.white); // MD 8/7
  673.         // shouldn't be necessary, but it is!
  674.         setLayout(new FlowLayout(FlowLayout.CENTER,2,2));
  675.  
  676.         // INPUT/OUTPUT/PATTERN
  677.  
  678.  
  679.         Panel creditPanel = new Panel();
  680.         label1=new Label("Decimal Number Formatting Demo",
  681.                          Label.CENTER);
  682.         label1.setFont(Utility.titleFont);
  683.         creditPanel.add(label1);
  684.  
  685.         label13=new Label(creditString, Label.CENTER);
  686.         label13.setFont(Utility.creditFont);
  687.         creditPanel.add(label13);
  688.         Utility.fixGrid(creditPanel,1);
  689.  
  690.         add(creditPanel);
  691.  
  692.         // IO
  693.         Panel ioPanel = new Panel();
  694.  
  695.         label3=new Label("New Number", Label.RIGHT);
  696.         label3.setFont(Utility.labelFont);
  697.         ioPanel.add(label3);
  698.  
  699.         outputText=new TextField(FIELD_COLUMNS);
  700.         outputText.setFont(Utility.editFont);
  701.         ioPanel.add(outputText);
  702.  
  703.         /*Panel gap1 = new Panel();
  704.         gap1.setLayout(null);
  705.         gap1.resize(5,5);
  706.         gap1.add(outputText);
  707.  
  708.         Panel gap2 = new Panel();
  709.         gap2.setLayout(null);
  710.         gap2.resize(5,5);
  711.         gap2.add(outputText);
  712.         */
  713.  
  714.         label2=new Label("Java 1.0 Number", Label.RIGHT);
  715.         label2.setFont(Utility.labelFont);
  716.         ioPanel.add(label2);
  717.  
  718.         inputText=new TextField(FIELD_COLUMNS);
  719.     inputText.addKeyListener(this);
  720.         inputText.setFont(Utility.editFont);
  721.         ioPanel.add(inputText);
  722.  
  723.         label4=new Label("Pattern", Label.RIGHT);
  724.         label4.setFont(Utility.labelFont);
  725.         ioPanel.add(label4);
  726.  
  727.         patternText=new TextField(FIELD_COLUMNS);
  728.     patternText.addKeyListener(this);
  729.         patternText.setFont(Utility.editFont);
  730.         ioPanel.add(patternText);
  731.  
  732.         ioPanel.add(new Label(" "));
  733.  
  734.         getLocalized=new Checkbox("Localized Pattern");
  735.     getLocalized.addItemListener(this);
  736.         getLocalized.setFont(Utility.labelFont);
  737.         ioPanel.add(getLocalized);
  738.  
  739.         Utility.fixGrid(ioPanel,2);
  740.         add(ioPanel);
  741.  
  742.          // LOCALE
  743.  
  744.         localePanel=new Panel();
  745.  
  746.         localeLabel=new Label("Locale:");
  747.         localeLabel.setFont(Utility.labelFont);
  748.         localePanel.add("loc",localeLabel);
  749.  
  750.         localeMenu= new Choice();
  751.     localeMenu.addItemListener(this);
  752.         localeMenu.setFont(Utility.choiceFont);
  753.         localePanel.add(localeMenu);
  754.  
  755.         // FORMAT CHECKS
  756.  
  757.         group1= new CheckboxGroup();
  758.  
  759.         getInstance=new Checkbox("Default Format",group1, false);
  760.     getInstance.addItemListener(this);
  761.         getInstance.setFont(Utility.labelFont);
  762.         localePanel.add(getInstance);
  763.  
  764.         getCurrency=new Checkbox("Currency Format",group1, true);
  765.     getCurrency.addItemListener(this);
  766.         getCurrency.setFont(Utility.labelFont);
  767.         localePanel.add(getCurrency);
  768.  
  769.         getPercent=new Checkbox("Percent Format",group1, false);
  770.     getPercent.addItemListener(this);
  771.         getPercent.setFont(Utility.labelFont);
  772.         localePanel.add(getPercent);
  773.  
  774.         Utility.fixGrid(localePanel,1);
  775.  
  776.         Panel bottomPanel = new Panel();
  777.  
  778.         bottomPanel.add(localePanel);
  779.  
  780.         BorderPanel x = new BorderPanel();
  781.         x.setBackground(Color.lightGray);
  782.         x.setLayout(null);
  783.         x.setSize(8,130);
  784.         bottomPanel.add(x);
  785.  
  786.         // PARAMETERS
  787.  
  788.         Panel parameterPanel = new Panel();
  789.  
  790.         parameterPanel.add(new Label(" "));
  791.  
  792.         label11=new Label("Min", Label.CENTER);
  793.         label11.setFont(Utility.labelFont);
  794.         parameterPanel.add(label11);
  795.  
  796.         label12=new Label("Max", Label.CENTER);
  797.         label12.setFont(Utility.labelFont);
  798.         parameterPanel.add(label12);
  799.  
  800.  
  801.         label9=new Label("Integers", Label.RIGHT);
  802.         label9.setFont(Utility.labelFont);
  803.         parameterPanel.add(label9);
  804.  
  805.         intMin=new TextField(4);
  806.     intMin.addKeyListener(this);
  807.         intMin.setFont(Utility.editFont);
  808.         parameterPanel.add(intMin);
  809.  
  810.         intMax=new TextField(4);
  811.     intMax.addKeyListener(this);
  812.         intMax.setFont(Utility.editFont);
  813.         parameterPanel.add(intMax);
  814.  
  815.  
  816.         label10=new Label("Decimals", Label.RIGHT);
  817.         label10.setFont(Utility.labelFont);
  818.         parameterPanel.add(label10);
  819.  
  820.         decMin=new TextField(4);
  821.     decMin.addKeyListener(this);
  822.         decMin.setFont(Utility.editFont);
  823.         parameterPanel.add(decMin);
  824.  
  825.         decMax=new TextField(4);
  826.     decMax.addKeyListener(this);
  827.         decMax.setFont(Utility.editFont);
  828.         parameterPanel.add(decMax);
  829.  
  830.  
  831.         parameterPanel.add(new Label(" "));
  832.  
  833.         label7=new Label("Prefix", Label.CENTER);
  834.         label7.setFont(Utility.labelFont);
  835.         parameterPanel.add(label7);
  836.  
  837.         label8=new Label("Suffix", Label.CENTER);
  838.         label8.setFont(Utility.labelFont);
  839.         parameterPanel.add(label8);
  840.  
  841.  
  842.         label6=new Label("Positive", Label.RIGHT);
  843.         label6.setFont(Utility.labelFont);
  844.         parameterPanel.add(label6);
  845.  
  846.         posPrefix=new TextField(4);
  847.     posPrefix.addKeyListener(this);
  848.         posPrefix.setFont(Utility.editFont);
  849.         parameterPanel.add(posPrefix);
  850.  
  851.         posSuffix=new TextField(4);
  852.     posSuffix.addKeyListener(this);
  853.         posSuffix.setFont(Utility.editFont);
  854.         parameterPanel.add(posSuffix);
  855.  
  856.  
  857.         label5=new Label("Negative", Label.RIGHT);
  858.         label5.setFont(Utility.labelFont);
  859.         parameterPanel.add(label5);
  860.  
  861.         negPrefix=new TextField(4);
  862.     negPrefix.addKeyListener(this);
  863.         negPrefix.setFont(Utility.editFont);
  864.         parameterPanel.add(negPrefix);
  865.  
  866.         negSuffix=new TextField(4);
  867.     negSuffix.addKeyListener(this);
  868.         negSuffix.setFont(Utility.editFont);
  869.         parameterPanel.add(negSuffix);
  870.  
  871.         Utility.fixGrid(parameterPanel,3);
  872.  
  873.         bottomPanel.add(parameterPanel);
  874.         Utility.fixGrid(bottomPanel,3);
  875.         Utility.setInsets(bottomPanel,x,new Insets(20,20,2,2));
  876.         add(bottomPanel);
  877.  
  878.         Panel copyrightPanel = new Panel();
  879.         addWithFont (copyrightPanel,new Label(copyrightString, Label.LEFT),
  880.                      Utility.creditFont);
  881.         addWithFont (copyrightPanel,new Label(copyrightString2, Label.LEFT),
  882.                      Utility.creditFont);
  883.         Utility.fixGrid(copyrightPanel,1);
  884.         add(copyrightPanel);
  885.  
  886.  
  887.     }
  888.  
  889.     private String toString( int anInt )
  890.     {
  891.         Integer intObj = new Integer(anInt);
  892.         return intObj.toString();
  893.     }
  894.  
  895.     private int parseIntField(TextComponent text) {
  896.         int result = 0;
  897.         try {
  898.             Integer value = new Integer(workaround(text.getText()));
  899.             result = value.intValue();
  900.         }
  901.         catch (NumberFormatException e) {
  902.         }
  903.         return result;
  904.     }
  905.  
  906.     private void intMinChanged() {
  907.         format.setMinimumIntegerDigits(parseIntField(intMin));
  908.         updateAttributes();
  909.         handleAttributeChange();
  910.     }
  911.     private void intMaxChanged() {
  912.         format.setMaximumIntegerDigits(parseIntField(intMax));
  913.         updateAttributes();
  914.         handleAttributeChange();
  915.     }
  916.     private void decMinChanged() {
  917.         format.setMinimumFractionDigits(parseIntField(decMin));
  918.         updateAttributes();
  919.         handleAttributeChange();
  920.     }
  921.     private void decMaxChanged() {
  922.         format.setMaximumFractionDigits(parseIntField(decMax));
  923.         updateAttributes();
  924.         handleAttributeChange();
  925.     }
  926.     private void negPrefixChanged() {
  927.         format.setNegativePrefix(workaround(negPrefix.getText()));
  928.         updateAttributes();
  929.         handleAttributeChange();
  930.     }
  931.     private void negSuffixChanged() {
  932.         format.setNegativeSuffix(workaround(negSuffix.getText()));
  933.         updateAttributes();
  934.         handleAttributeChange();
  935.     }
  936.     private void posPrefixChanged() {
  937.         format.setPositivePrefix(workaround(posPrefix.getText()));
  938.         updateAttributes();
  939.         handleAttributeChange();
  940.     }
  941.     private void posSuffixChanged() {
  942.         format.setPositiveSuffix(workaround(posSuffix.getText()));
  943.         updateAttributes();
  944.         handleAttributeChange();
  945.     }
  946.     /**
  947.      * Strips high bits, because of bug in Java
  948.      */
  949.     private static String workaround(String source) {
  950.         StringBuffer result = new StringBuffer();
  951.         for (int i = 0; i < source.length(); ++i)
  952.             result.append((char)(source.charAt(i) & 0xFF));
  953.         return result.toString();
  954.     }
  955.  
  956.     private void errorText(String s)
  957.     {
  958.         if (DEBUG)
  959.         {
  960.           System.out.println(s);
  961.         }
  962.     }
  963.  
  964.     private static final String creditString =
  965.         "v1.1a6, Demo:";
  966.     private static final String copyrightString =
  967.         "";
  968.     private static final String copyrightString2 =
  969.         "";
  970.     private static final int FIELD_COLUMNS = 35;
  971.  
  972.     private static final boolean DEBUG = false;
  973.  
  974.     private static boolean isLocalized = false;
  975.  
  976.     // private Choice formatter, locale;
  977.  
  978.     private DecimalFormat format;
  979.     private Locale[] locales;
  980.     private DemoApplet applet;
  981.  
  982. }
  983.